package com.example.superoa.controller;

import java.sql.Time;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.pojos.KqBanci;
import com.example.superoa.model.pojos.KqRecess;
import com.example.superoa.model.service.VacationService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
public class VacationController {
	@Autowired
	private VacationService v_biz;	
	//kq-vacation-manage table初始化
	@RequestMapping("vacation_selectAll")
	@ResponseBody
	public Map<String,Object> vacation_selectAll(int page,int rows){
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray json = v_biz.selectAll();
		int i=1;
		for (Object object : json) {
			JSONObject obj=(JSONObject) object;
			if(obj.get("RE_QI").equals(1)) {
				obj.put("qi", "已启用");
			}else {
				obj.put("qi", "未启用");
			}
			if(obj.getInteger("RE_CLASS")==1) {
				obj.put("class", "公司");
			}else {
				obj.put("class", "法定");
				obj.put("RE_KAI", obj.get("RE_KAI").toString().substring(5, obj.get("RE_KAI").toString().length()));
				obj.put("RE_JIE", obj.get("RE_JIE").toString().substring(5, obj.get("RE_JIE").toString().length()));
			}
			obj.put("sortable", i);
			i++;
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", json);
		return map;
	}
	//kq-vacation-manage 新增假期时判断是否有启用的相同时间的假期
	@RequestMapping("select-vacation")
	@ResponseBody
	public Integer selectvacation(Integer lei,Integer type,Date kai,Date jie,Integer id) {
		return v_biz.panduan(lei,type, kai, jie,id);
	}
	@RequestMapping("vacation_select")
	@ResponseBody
	public JSONArray vacation_selectAll(){
		JSONArray json = v_biz.selectAll();
		int i=1;
		for (Object object : json) {
			JSONObject obj=(JSONObject) object;
			if(obj.get("RE_QI").equals(1)) {
				obj.put("qi", "已启用");
			}else {
				obj.put("qi", "未启用");
			}
			if(obj.getInteger("RE_CLASS")==1) {
				obj.put("class", "公司");
			}else {
				obj.put("class", "法定");
				obj.put("RE_KAI", obj.get("RE_KAI").toString().substring(5, obj.get("RE_KAI").toString().length()));
				obj.put("RE_JIE", obj.get("RE_JIE").toString().substring(5, obj.get("RE_JIE").toString().length()));
			}
			obj.put("sortable", i);
		}
		return json;
	}
	//kq-vacation-manage 修改查询
	@RequestMapping("selectJiaId")
	@ResponseBody
	public JSONObject selectId(int id) {
		return v_biz.selectId(id);
	}
	//kq-vacation-manage 新增修改
	@PostMapping("add_jiaqi")
	public String addBanci(String RE_ID,String RE_NAME,Date RE_KAI,Date RE_JIE,Integer RE_CLASS,Integer RE_QI) {
		KqRecess pojo=new KqRecess();
		try {
			pojo.setReId(Integer.parseInt(RE_ID));
		} catch (Exception e) {
			// TODO: handle exception
		}
		pojo.setReKai(RE_KAI);
		pojo.setReJie(RE_JIE);
		pojo.setReName(RE_NAME);
		pojo.setReQi(RE_QI);
		pojo.setReClass(RE_CLASS);	
		v_biz.addjiqi(pojo);
		return "redirect:main-administrator:kq-attendance-rule";
	}
	
}
